<?php 
class Template {
  public static $layout_type = "liquid";
  public static $javascripts = array();
  public static $stylesheets = array("style");
  
  public static function Render($template, $vars=null)
  {
    $file_path = "template/$template.tpl.php";
    if (!file_exists($file_path))
      return "";
    if (is_array($vars) && !empty($vars)) {
      extract($vars);
    }
    ob_start();
    include $file_path;
    return ob_get_clean();
  }
  
  public static function AddJs($name){
    self::$javascripts[] = $name;
  }
  
  public static function AddCss($name){
    self::$stylesheets[] = $name;
  }
  
  public static function AddJsAndCss($name){
    self::$javascripts[] = $name;
    self::$stylesheets[] = $name;
  }
  
  public static function RenderAddedJsCss(){
    global $settings;
    
    $rendered = "";
    if (count(self::$stylesheets) > 0)
      foreach (self::$stylesheets as $css)
        $rendered .= "<link href=\"".$settings["base_url"]."/media/css/$css.css?".$settings["version"]."\" rel=\"stylesheet\" type=\"text/css\"  media=\"all\"  />\n";
    if (count(self::$javascripts) > 0)
      foreach (self::$javascripts as $js)
        $rendered .= "<script type=\"text/javascript\" src=\"".$settings["base_url"]."/media/js/$js.js?".$settings["version"]."\"></script>\n";
    return $rendered;
  }
  
  public static function RenderGlobalMsg(){
    $output = "";
    if (Util::HasValue($_SESSION['global_msg'])){
      $output = $_SESSION['global_msg'];
      $_SESSION['global_msg'] = "";
    }
    return '<div class="global_msg"><span>'.$output.'</span></div>';
  }
  
  public static function RenderCaptcha(){
    require_once($_SERVER['DOCUMENT_ROOT']. '/system/3rd_party/recaptchalib.php');
    $publickey = "6Ldivr0SAAAAABzTHvWjsEuLivHjUHi5TK4JefwQ";
    return recaptcha_get_html($publickey);
  }
  
  public static function IsValidCaptcha($recaptcha_challenge_field, $recaptcha_response_field){
    require_once($_SERVER['DOCUMENT_ROOT']. '/system/3rd_party/recaptchalib.php');
    $privatekey = "6Ldivr0SAAAAAOWJuFgbdRX9pKN6_2kOEFbMAvfI";
    $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $recaptcha_challenge_field,
                                $recaptcha_response_field);
    return $resp->is_valid;
  }
  
  public static function GetGravatar( $email, $img = false, $s = 60, $d = 'monsterid', $r = 'g', $atts = array() ) {
    $url = 'http://www.gravatar.com/avatar/';
    $url .= md5( strtolower( trim( $email ) ) );
    $url .= "?s=$s&d=$d&r=$r";
    if ( $img ) {
      $url = '<img src="' . $url . '"';
      foreach ( $atts as $key => $val )
        $url .= ' ' . $key . '="' . $val . '"';
      $url .= ' />';
    }
    return $url;
  }
  
  public static function RenderPagination($start, $total, $num_per_page, $url){
    $num_pages = ceil($total / $num_per_page);
    $current_page = floor($start / $num_per_page) + 1;
    $output = "";
    if ($current_page - 3 > 0)
      $output .= "<span> . . </span>";
    for ($i=-2; $i<=2; $i++){
      $p = $current_page + $i;
      $s = ($p - 1) * $num_per_page;
      if ($p >= 1 && $p <= $num_pages)
        $output .= "<a href=\"javascript: void(0);\" rel=\"$s\">$p</a>";
    }
    if ($current_page + 3 <= $num_pages)
      $output .= "<span> . . </span>";
    return "<div class=\"pagination\">$output</div>";
  }
  
  public static function FetchRawComments($tablename, $id){
    $_GET['t'] = $tablename;
    $_GET['id'] = $id;
    ob_start();
    include("_ajax/comment.php");
    return ob_get_clean();
  }
  
  public static function ChangeURLParam($variable_name, $value){
    
  }
  
  public static function RenderAd($name){
    $ads['gg468'] = '<script type="text/javascript"><!--
              google_ad_client = "ca-pub-9820855084114914";
              /* maple net 468 */
              google_ad_slot = "2528106816";
              google_ad_width = 468;
              google_ad_height = 60;
              //-->
              </script>
              <script type="text/javascript"
              src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
              </script>';
    $ads['gg300'] = '<script type="text/javascript"><!--
              google_ad_client = "ca-pub-9820855084114914";
              /* maple net */
              google_ad_slot = "2779923106";
              google_ad_width = 300;
              google_ad_height = 250;
              //-->
              </script>
              <script type="text/javascript"
              src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
              </script>';
    $ads['gg160'] = '<script type="text/javascript"><!--
              google_ad_client = "ca-pub-9820855084114914";
              /* maple net 160 */
              google_ad_slot = "6817772381";
              google_ad_width = 160;
              google_ad_height = 600;
              //-->
              </script>
              <script type="text/javascript"
              src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
              </script>';
    $sm_section_code = "1005133";
    if ($_SERVER["SERVER_NAME"] == "www.maplestage.net")
      $sm_section_code = "1762572";
    $ads['sm468'] = '<iframe allowtransparency="true" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="468" height="60" src="http://ads.smowtion.com/st?ad_size=468x60&section='.$sm_section_code.'"></iframe>';
    $ads['sm300'] = '<iframe allowtransparency="true" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="300" height="250" src="http://ads.smowtion.com/st?ad_size=300x250&section='.$sm_section_code.'"></iframe>';
    $ads['sm160'] = '<iframe allowtransparency="true" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="160" height="600" src="http://ads.smowtion.com/st?ad_size=160x600&section='.$sm_section_code.'"></iframe>';
    
    return $ads[$name];
  }
  
}
?>